package top.aigoo.project01.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.aigoo.project01.entity.LocalAuth;
import top.aigoo.project01.entity.dto.LocalAuthExecution;
import top.aigoo.project01.enums.LocalAuthStateEnum;
import top.aigoo.project01.mapper.LocalAuthMapper;
import top.aigoo.project01.service.LocalAuthService;
import top.aigoo.project01.utils.MD5;

import java.util.Date;

/**
 * @Description: 请填写该类完成的功能
 * @Author: Mr.OYSHUO.欧阳大叔  mail:1642302522@qq.com
 * @Version 1.0.0
 * @Date: 2021/9/12 22:16
 */
@Service
public class LocalAuthServiceImpl implements LocalAuthService {

    @Autowired
    private LocalAuthMapper localAuthMapper;

    /**
     * 登录
     */
    @Override
    public LocalAuth getLocalAuthByUsernameAndPwd(String userName, String password) {
        return localAuthMapper.queryLocalByUserNameAndPwd(userName, MD5.getMd5(password));
    }

    /**
     * 获取登录账户信息
     */
    @Override
    public LocalAuth getLocalAuthByUserId(long userId) {
        return localAuthMapper.queryLocalByUserId(userId);
    }

    /**
     * 绑定账户
     */
    @Override
    @Transactional
    public LocalAuthExecution bindLocalAuth(LocalAuth localAuth) throws RuntimeException {
        //空值判断,传入localAuth账号密码，用回信息特别是userId不能为空,否则直接抛出异常
        if (localAuth == null || localAuth.getPassword() == null
                || localAuth.getUserName() == null
                || localAuth.getPersonInfo() == null
                || localAuth.getPersonInfo().getUserId() == null) {
            return new LocalAuthExecution(LocalAuthStateEnum.NULL_AUTH_INFO);
        }
        //先查一下用户是否已绑定过平台账号
        LocalAuth tempAuth = localAuthMapper.queryLocalByUserId(localAuth.getPersonInfo().getUserId());
        if (tempAuth != null) {
            //如果已经绑定则直接退出，以保证平台账号的唯一性
            return new LocalAuthExecution(LocalAuthStateEnum.ONLY_ONE_ACCOUNT);
        }
        //如果之前没有绑定平台账号，则创建一个平台账号与该用户进行绑定
        try {
            //localAuth.setCreateTime(new Date());
            //localAuth.setUpdateTime(new Date());
            //对密码进行MD5加密
            localAuth.setPassword(MD5.getMd5(localAuth.getPassword()));
            // 创建登录账号信息
            int effectedNum = localAuthMapper.insertLocalAuth(localAuth);
            //判断创建是否成功
            if (effectedNum <= 0) {
                throw new RuntimeException("帐号绑定失败");
            } else {
                return new LocalAuthExecution(LocalAuthStateEnum.SUCCESS, localAuth);
            }
        } catch (Exception e) {
            throw new RuntimeException("insertLocalAuth error: " + e.getMessage());
        }

    }

    /**
     * 修改密码
     */
    @Override
    @Transactional
    public LocalAuthExecution modifyLocalAuth(Long userId,
                                              String userName,
                                              String password,
                                              String newPassword)
            throws RuntimeException {
        //非空判断,判断传入的用户id,账号,新旧密码是否为空,新旧密码是否相同，若不满足条件则返回错误信息
        if (userId != null && userName != null && password != null
                && newPassword != null && !password.equals(newPassword)) {
            try {
                //更新密码，并对新密码进行MD5加密
                int effectedNum = localAuthMapper.updateLocalAuth(userId, userName, MD5.getMd5(password),
                        MD5.getMd5(newPassword), new Date());
                //判断更新是否成功
                if (effectedNum <= 0) {
                    throw new RuntimeException("更新密码失败");
                }
                return new LocalAuthExecution(LocalAuthStateEnum.SUCCESS);
            } catch (Exception e) {
                throw new RuntimeException("更新密码失败:" + e.toString());
            }
        } else {
            return new LocalAuthExecution(LocalAuthStateEnum.NULL_AUTH_INFO);
        }
    }
}
